package com.yhb.controller;

import com.yhb.model.Card;
import com.yhb.model.Plan;
import com.yhb.model.User;
import com.yhb.service.ChannelFlowService;
import com.yhb.service.PlanService;
import me.edzh.spring.bootstrap.annotations.CurrentUser;
import me.edzh.spring.bootstrap.annotations.EncryptedRequestBody;
import me.edzh.spring.bootstrap.annotations.NeedAuth;
import me.edzh.spring.bootstrap.annotations.SignedRequestBody;
import me.edzh.spring.bootstrap.model.Response;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/plan")
public class PlanController {

    @Autowired
    private ChannelFlowService channelFlowService;

    @Autowired
    private PlanService planService;

    @PostMapping("/pay/gen")
    @NeedAuth
    public Response<Plan> genPlan(@CurrentUser User user, @SignedRequestBody Plan plan) {
        return new Response<>(channelFlowService.aiGenerateFlows(user, plan.getCardId(), plan.getRemaining(), plan.getRemaining()));
    }

    @PostMapping("/pay/create")
    @NeedAuth
    public Response createPlan(@CurrentUser User user, @EncryptedRequestBody Plan plan) {
        planService.addPlan(plan, user);
        return Response.ok();
    }

    @GetMapping("/pay")
    @NeedAuth
    public Response<List<Plan>> getPlanList(@CurrentUser User user) {
        return Response.ok(planService.userReturnPlanList(user.getId()));
    }

    @GetMapping("/pay/{planId}")
    @NeedAuth
    public Response<Plan> getReturnPlan(@CurrentUser User user, @PathVariable("planId")String planId) {
        return Response.ok(planService.returnPlan(planId, user));
    }
}
